/*
 * 文件名：RoleInfoServiceImp.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：ozm
 * 修改时间：2013-1-28
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.ets.sys.role.service.imp;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.dao.DataAccessException;

import com.wootide.ets.common.ETSRuntimeException;
import com.wootide.ets.common.ErrorCodes;
import com.wootide.ets.common.Page;
import com.wootide.ets.common.QueryCondition;
import com.wootide.ets.common.QueryResults;
import com.wootide.ets.common.Result;
import com.wootide.ets.common.util.CommonUtil;
import com.wootide.ets.log.syslog.RunLog;
import com.wootide.ets.sys.rightinfo.pojo.AuthoritiesInfo;
import com.wootide.ets.sys.rightinfo.service.AuthoritiesInfoService;
import com.wootide.ets.sys.role.dao.RoleInfoDao;
import com.wootide.ets.sys.role.pojo.RoleInfo;
import com.wootide.ets.sys.role.service.RoleInfoService;
import com.wootide.ets.sys.role.transaction.RoleInfoTransaction;

/**
 * 用户业务层实现层
 * 〈功能详细描述〉
 * @author ozm
 * @version 2013-1-28
 * @see RoleInfoServiceImp
 * @since 2013-1-28下午2:20:01
 */
public class RoleInfoServiceImp implements RoleInfoService
{
    /**
     *  注入运行日志
     */
    private RunLog runLog;
    
    /**
     * 角色事务层实例
     */
    private RoleInfoTransaction roleInfoTransaction;
    
    /**
     * 权限管理事务层实例
     */
    private AuthoritiesInfoService authoritiesInfoService;
    
    /**
     * 角色数据访问层实例
     */
    private RoleInfoDao roleInfoDao;
    
    @SuppressWarnings("rawtypes")
    public Result queryAuthoritiesInfos(Integer innerIndex,
            HttpServletRequest request)
    {
        // 打印方法入口参数
        runLog.debug("innerIndex=" + innerIndex);
        
        // 定义返回结果
        Result result = new Result();
        
        try
        {
            List authoritiesInfos = roleInfoDao.findAuthoritiesInfosByRoleId(innerIndex);
            
            Set menuSet = new HashSet();
            
            List menus = new ArrayList();
            
            for (Iterator iterator = authoritiesInfos.iterator(); iterator.hasNext();)
            {
                AuthoritiesInfo authoritiesInfo = (AuthoritiesInfo)iterator.next();
                Result menuResult = authoritiesInfoService.queryMenus(authoritiesInfo.getAuthorityId(), request);
                
                //合并所有的菜单List
                menuSet.addAll(menuResult.getResultList());
            }
            
            menus = new ArrayList(menuSet);
            
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            result.setResultList(menus);
        }
        catch (DataAccessException e)
        {
            runLog.error("Find AuthoritiesInfos Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (ETSRuntimeException e)
        {
            //自定义异常
            runLog.info("Mod AuthoritiesInfos! Input Data:" + innerIndex
                    + " Error Message:" + e.getMessage());
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            runLog.error("Find AuthoritiesInfos Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        
        return result;
    
    }

    /**
     * 
     * Description: 分页查询
     * Implement: <br>
     * @param page 分页参数
     * @param queryCondition 查询条件
     * @see
     * @return Result 结果对象
     * @author ozm2013-1-29下午1:23:32
     */
    public Result queryForPages(Page page, QueryCondition queryCondition)
    {
        // 打印方法入口参数
        runLog.debug("page=" + page);
        runLog.debug("queryCondition=" + queryCondition);
        
        // 定义返回结果
        Result result = new Result();
        try
        {
            queryCondition = null == queryCondition? new QueryCondition() : queryCondition;
            queryCondition.setObjClass(RoleInfo.class);
            QueryResults queryResults = roleInfoTransaction.queryForPages(page,
                    queryCondition);
            result.setResultPojo(queryResults);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            runLog.info("Query Role Success!");
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Query Role Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        
        catch (Exception e)
        {
            //异常
            runLog.error("Query Role Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }
    
    public Result viewAllRoles()
    {
        Result result = new Result();
        try
        {
            QueryResults queryResulets = roleInfoTransaction.viewAllRoles();
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            result.setResultList(queryResulets.getResults());
        }
        catch (DataAccessException e)
        {
            runLog.error("Find One Role Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (ETSRuntimeException e)
        {
            //自定义异常
            runLog.info("Find All Role Failed! Input Data: Error Message:" + e.getMessage());
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            runLog.error("Find All Role Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }

    /**
     * 
     * Description: 添加一个角色
     * Implement: <br>
     * @param roleInfo 要添加的角色信息
     * @param request HttpServletRequest
     * @see
     * @return Result 结果对象
     * @author ozm2013-1-29下午1:26:06
     */
    public Result addOneRole(RoleInfo roleInfo,HttpServletRequest request)
    {
        // 打印方法入口参数
        runLog.debug("roleInfo=" + roleInfo);
        Result result = new Result();
        try
        {
            roleInfoTransaction.addOneRole(roleInfo);
            runLog.info("Add Role Success! Input data:" + roleInfo, request);
            
            // 设置返回码为成功
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Add Role Failed! Input data:" + roleInfo, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (ETSRuntimeException e)
        {
            //运行时异常
            runLog.info("Add Role Failed! Input Data:" + roleInfo
                    + " Error Message:" + e.getMessage(), request);
            
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            //未知异常
            runLog.error("Add Role Failed!Input data:" + roleInfo, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }
    
    /**
     * 
     * Description: 修改一个角色信息
     * Implement: <br>
     * @param user 要修点的角色信息
     * @param request HttpServletRequest
     * @see
     * @return Result 结果对象
     * @author ozm2013-1-29下午1:27:37
     */
    public Result modOneRole(RoleInfo roleInfo, HttpServletRequest request)
    {
        // 打印方法入口参数
        runLog.debug("roleInfo=" + roleInfo);
        Result result = new Result();
        try
        {
            RoleInfo oldRoleInfo = roleInfoTransaction.modOneRole(roleInfo);
            runLog.info("Mod Role Success! Input data:" + roleInfo
                    + "|oldDate:" + oldRoleInfo, request);
            
            // 设置返回码为成功
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Mod Role Failed! Input data:" + roleInfo, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (ETSRuntimeException e)
        {
            //自定义异常
            runLog.info("Mod Role Failed! Input Data:" + roleInfo
                    + " Error Message:" + e.getMessage(), request);
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            //位置异常
            runLog.error("Mod Role Failed!Input data:" + roleInfo, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }
    
    /**
     * 
     * Description: 通过索引查询一个角色
     * Implement: <br>
     * @param innerIndex 要查询角色的roleId
     * @see
     * @return Result 结果对象
     * @author ozm2013-1-29下午3:55:17
     */
    public Result viewOneRole(Integer innerIndex)
    {

        // 打印方法入口参数
        runLog.debug("innerIndex=" + innerIndex);

        Result result = new Result();
        try
        {
            RoleInfo roleInfo = roleInfoTransaction.viewOneRole(innerIndex);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            result.setResultPojo(roleInfo);
        }
        catch (DataAccessException e)
        {
            runLog.error("Find One Role Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (ETSRuntimeException e)
        {
            //自定义异常
            runLog.info("Mod Role Failed! Input Data:" + innerIndex
                    + " Error Message:" + e.getMessage());
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            runLog.error("Find One Role Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    
    }
    
    /**
     * 
     * Description: 删除角色
     * Implement: <br>
     * @param innerIndexs 要删除的角色id列表
     * @param request HttpServletRequest
     * @see
     * @return Result 结果对象
     * @author ozm2013-1-29下午4:17:56
     */
    public Result delRoles(Integer[] innerIndexs, HttpServletRequest request)
    {
        // 打印方法入口参数
        runLog.debug("innerIndexs = " + CommonUtil.toString(innerIndexs));
        
        Result result = new Result();
        
        result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        // 定义删除失败的条数
        int delErrorNum = 0;
        
        for (Integer innerIndex : innerIndexs)
        {
            RoleInfo roleInfo = null;
            try
            {
                // 调用删除路由事务层
                roleInfo = roleInfoTransaction.delOneRole(innerIndex);
                runLog.info("Delete role Success! roleInfo = " + roleInfo,
                        request);
                
            }
            catch (DataAccessException e)
            {
                //数据库异常
                delErrorNum++;
                runLog.error("Delete Role Failed! Innerindex = " + innerIndex,
                        e,
                        request);
                
                result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
                
            }
            catch (ETSRuntimeException e)
            {
                //运行时异常
                runLog.info("Delete Role Failed! Innerindex = " + innerIndex
                        + " Error Message:" + e.getMessage(), request);
                result.setResultCode(e.getErrorCode());
                
            }
            catch (Exception e)
            {
                //未知异常
                delErrorNum++;
                runLog.error("Delete Role Failed!  Innerindex = " + innerIndex,
                        e,request);
                result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
                
            }
        }
        // 返回失败的条数
        result.setDelErrorNum(delErrorNum);
        return result;
    }

    /**
     * 
     * Description: 新增角色唯一性验证
     * Implement: <br>
     * @param content 角色名称
     * @return
     * @see
     * @return Result 结果对象
     * @author ozm2013-1-31下午1:41:22
     */
    public Result checkContentWhenAdd(String content)
    {
        
        // 打印方法入口参数
        runLog.debug("content=" + content);

        int num = roleInfoDao.findNumByRoleName(content);
        Result result = new Result();
        // 如果已存在
        if (num > 0)
        {
            runLog.info("Illegal!Role Has Existed! Input Data:" + content);
            
            result.setResultCode(ErrorCodes.Sys.Role.ROLEEXIST);
        }
        // 如果不存在
        else
        {
            runLog.info("Legal!User Not Existed! Input Data:" + content);
           
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        }
        return result;
    }
    
    public RoleInfoTransaction getRoleInfoTransaction()
    {
        return roleInfoTransaction;
    }

    public void setRoleInfoTransaction(RoleInfoTransaction roleInfoTransaction)
    {
        this.roleInfoTransaction = roleInfoTransaction;
    }

    public RoleInfoDao getRoleInfoDao()
    {
        return roleInfoDao;
    }

    public void setRoleInfoDao(RoleInfoDao roleInfoDao)
    {
        this.roleInfoDao = roleInfoDao;
    }

    public RunLog getRunLog()
    {
        return runLog;
    }

    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }

    public void setAuthoritiesInfoService(
            AuthoritiesInfoService authoritiesInfoService)
    {
        this.authoritiesInfoService = authoritiesInfoService;
    }

}
